package io.renren.modules.appReport.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baidu.ueditor.define.AppInfo;
import io.renren.common.utils.*;
import io.renren.modules.appReport.model.AppFinanceAuditExcelBean;
import io.renren.modules.appReport.model.AppFinanceAuditExportExcel;
import io.renren.modules.recorderReport.entity.ReportInfoEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.appReport.entity.AppInfoEntity;
import io.renren.modules.appReport.service.AppInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-11-13 17:30:33
 */
@RestController
@RequestMapping("appReport/appinfo")
public class AppInfoController {
    @Autowired
    private AppInfoService appInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("appReport:appinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AppInfoEntity> appInfoList = appInfoService.queryList(query);
        int total = appInfoService.queryList(query.noLimit()).size();

        PageUtils pageUtil = new PageUtils(appInfoList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 违法审核列表
     */
    @RequestMapping("/list/check")
    @RequiresPermissions("appReport:appinfo:list")
    public R listCheck(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<AppInfoEntity> appInfoList = appInfoService.queryListCheck(query);
        //查询总数
        int total = appInfoService.queryListCheck(query.noLimit()).size();
        PageUtils pageUtil = new PageUtils(appInfoList, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 违法审核列表
     */
    @RequestMapping("/list/wfsh")
    @RequiresPermissions("appReport:appinfo:list")
    public R listWfsh(@RequestParam Map<String, Object> params) {
        //查询列表数据
        params.put("type", "jtwf");
        Query query = new Query(params);
        List<AppInfoEntity> appInfoList = appInfoService.queryListJtwf(query);
        int total = appInfoService.queryListJtwf(query.noLimit()).size();
        PageUtils pageUtil = new PageUtils(appInfoList, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 财务审核信息列表
     */
    @RequestMapping("/listReportFinanceAuditInfo")
    @RequiresPermissions("appReport:appinfo:listReportFinanceAuditInfo")
    public R listReportFinanceAuditInfo(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AppInfoEntity> appInfoList = appInfoService.queryListReportFinanceAuditInfo(query);
        int total = appInfoService.queryListReportFinanceAuditInfo(query.noLimit()).size();

        PageUtils pageUtil = new PageUtils(appInfoList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("appReport:appinfo:info")
    public R info(@PathVariable("id") String id) {
        AppInfoEntity appInfo = appInfoService.queryObject(id);
        return R.ok().put("appInfo", backAppInfo(appInfo));
    }

    /**
     * 获取车辆信息
     */
    @RequestMapping("/info1")
    @RequiresPermissions("appReport:appinfo:info")
    public R info1(@RequestBody AppInfoEntity appInfoEntity) {
        AppInfoEntity appInfo = appInfoService.queryObject(appInfoEntity);
        return R.ok().put("appInfo", backAppInfo(appInfo));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("appReport:appinfo:save")
    public R save(@RequestBody AppInfoEntity appInfo) {
        appInfoService.save(appInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("appReport:appinfo:update")
    public R update(@RequestBody AppInfoEntity appInfo) {
        appInfoService.update(appInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("appReport:appinfo:delete")
    public R delete(@RequestBody String[] ids) {
        appInfoService.deleteBatch(ids);

        return R.ok();
    }


    /**
     * 审核
     */
    @RequestMapping("/appFinanceAudit/{id}")
    @RequiresPermissions("appReport:appinfo:appFinanceAudit")
    public R reportFinanceAudit(@PathVariable("id") String id) {

        AppInfoEntity appInfo = appInfoService.queryObject(id);
        appInfo.setState("5");
        appInfoService.update(appInfo);
        return R.ok();
    }

    /**
     * 导出列表
     */
    @RequestMapping("/exportReportFinanceAudit")
    public void exportReportFinanceAudit(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {

        Query query = new Query(params);

        List<AppFinanceAuditExcelBean> list = new ArrayList<>();
        list = appInfoService.listReportFinanceAuditExcel(query);


        //设置excel列名及宽度
        AppFinanceAuditExportExcel<AppFinanceAuditExcelBean> ex = new AppFinanceAuditExportExcel<>();
        String[] headers = {"姓名", "身份证号", "联系方式", "银行卡号", "上报时间", "上报地点", "违法车牌", "违法地点", "行驶方向", "行驶车道", "违法类型", "违法代码", "车辆品牌", "车辆类型", "车辆颜色", "车辆型号", "状态"};
        int[] columnWidths = {4000, 6000, 5000, 6000, 5000, 8000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000};

        //导出excel
        ex.exportExcel("举报举证信息列表", headers, columnWidths, list, response);

    }

    /**
     * App信息
     *
     * @param appInfoEntity
     * @return appInfoEntity
     */
    private static AppInfoEntity backAppInfo(AppInfoEntity appInfoEntity) {
        if (appInfoEntity.getTime() != null) {
            appInfoEntity.setTimeStr(DateUtil.dateFormat(appInfoEntity.getTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        appInfoEntity.setTypeStr(AppInfoController.stringTransition(appInfoEntity.getType()));
        appInfoEntity.setStateStr(AppInfoController.stateTransition(appInfoEntity.getState()));
        return appInfoEntity;
    }


    /**
     * 上报类型字符转换
     *
     * @param str
     * @return
     */
    private static String stringTransition(String str) {
        if ("jtwf".equals(str)) {
            return "交通违法";
        }
        if ("jtsg".equals(str)) {
            return "交通事故";
        }
        if ("jtyd".equals(str)) {
            return "交通拥堵";
        }
        if ("sssh".equals(str)) {
            return "设施损坏";
        }
        return "其他";
    }

    /**
     * 审核类型字符转换
     *
     * @param str
     * @return
     */
    public static String stateTransition(String str) {
        if ("0".equals(str)) {
            return "未审核";
        }
        if ("1".equals(str)) {
            return "初审核通过";
        }
        if ("2".equals(str)) {
            return "初审核不通过";
        }
        if ("3".equals(str)) {
            return "审核通过";
        }
        if ("4".equals(str)) {
            return "审核无效";
        }
        if ("5".equals(str)) {
            return "财务已审核";
        }
        if ("6".equals(str)) {
            return "红包已发放";
        }
        return "其他";
    }

}
